<?php

namespace App\BO\Order\CreateOrder;

use App\Entities\CartItemEntity;
use App\Entities\ProductEntity;
use App\Entities\ProductSkuEntity;
use App\Utils\Assert;
use App\Utils\MoneyFormatUtil;

class CreateOrderFactory
{
    /**
     * @param ProductSkuEntity $productSkuEntity
     * @param ProductEntity $productEntity
     * @return ProductInfo
     */
    public static function getProductInfo(ProductEntity $productEntity, ProductSkuEntity $productSkuEntity)
    {
        $productInfo = new ProductInfo();
        $productInfo->setProductId($productEntity->getId());
        $productInfo->setTitle($productEntity->getTitle());
        $productInfo->setProductSkuId($productSkuEntity->getId());
        $productInfo->setSkuTitle($productSkuEntity->getTitle());
        $productInfo->setPrice(MoneyFormatUtil::dollarToPenny($productSkuEntity->getPrice()));
        $productInfo->setOldPrice(MoneyFormatUtil::dollarToPenny($productSkuEntity->getOldPrice()));
        $productInfo->setProductUnitId($productSkuEntity->getProductUnitId());
        return $productInfo;
    }

    /**
     * @param int $productId
     * @param int $productSkuId
     * @return ProductInfo
     * @throws \Exception
     */
    public static function getProductInfoById(int $productId, int $productSkuId)
    {
        $productEntity = ProductEntity::find($productId);
        Assert::notNull($productEntity);

        $productSkuEntity = ProductSkuEntity::find($productSkuId);
        Assert::notNull($productSkuEntity);

        return self::getProductInfo($productEntity, $productSkuEntity);
    }

    /**
     * @param CartItemEntity $cartItemEntity
     * @return ProductInfo
     * @throws \Exception
     */
    public static function getProductInfoByCartItemEntity(CartItemEntity $cartItemEntity)
    {
        return self::getProductInfoById($cartItemEntity->getProductId(), $cartItemEntity->getProductSkuId());
    }
}
